// <copyright file="PrintInstruction.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using System;
    using System.Collections.Generic;
    using Eco.ObjectRepresentation;
    using Eco.UmlCodeAttributes;

    /// <summary>
    /// PrintInstruction: Instruction that prints line output
    /// </summary>
    public partial class PrintInstruction
    {
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="variableList">The variable list.</param>
        /// <returns>Return value of executed function</returns>
        [UmlElement(Id = "991a7bee-1324-4f75-8da3-4a54d0dcefae")]
        protected internal override IElement ExecuteFunction(IEcoServiceProvider serviceProvider, IModifiableVariableList variableList)
        {
            IEcoScriptLanguageService ecoScriptLanguageService = serviceProvider.GetEcoService<IEcoScriptLanguageService>();
            IElement element = this.OCLInstruction.ExecuteFunction(serviceProvider, variableList);
            if (element != null)
            {
                IElementCollection elementList = element.Element.GetAsCollection();
                foreach (IElement loopElement in elementList)
                {
                    if (loopElement.AsObject != null)
                    {
                        ecoScriptLanguageService.WriteLine(loopElement.AsObject.ToString());
                    }
                    else
                    {
                        ecoScriptLanguageService.WriteLine(string.Empty);
                    }
                }
            }
            else
            {
                ecoScriptLanguageService.WriteLine(string.Empty);
            }

            return null;
        }

        /// <summary>
        /// Inits the specified tokens.
        /// </summary>
        /// <param name="tokens">The tokens.</param>
        [UmlElement(Id = "770926ef-4bd3-48cb-9d05-3a662c268dc1")]
        protected internal override void Init(Dictionary<IList<EslToken>, Type> tokens)
        {
            List<EslToken> tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "Print"));
            tokens.Add(tokenList, this.GetType());
        }

        /// <summary>
        /// Parses the specified token list.
        /// </summary>
        /// <param name="tokenList">The token list.</param>
        /// <param name="ecoScript">The eco script.</param>
        /// <param name="fullExpression">if set to <c>true</c> [full expression].</param>
        [UmlElement(Id = "b8bdf709-5a21-4089-bddb-eedcd94dbcbd")]
        protected internal override void Parse(IList<EslToken> tokenList, EcoScript ecoScript, bool fullExpression)
        {
            EslToken token = GetNextToken(tokenList);
            RememberEcoScript(ecoScript, token);

            EslInstruction instruction = ecoScript.ReadNextInstruction(tokenList, fullExpression);

            if (!(instruction is InstructionWithOcl))
            {
                throw new EslException("Instruction with OCL expected at " + token.ToString(), token);
            }

            this.OCLInstruction = instruction as InstructionWithOcl;
        }
    }
}